package com.atguigui.leetcode;

/**
 * 1945.字符串转化后的各位数字之和
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/12/15 14:02
 */
public class P1945SumOfDigitsOfStringAfterConvert {
    public static void main(String[] args) {
        Solution solution = new P1945SumOfDigitsOfStringAfterConvert().new Solution();
        // TO TEST
    }

    class Solution {
        public int getLucky(String s, int k) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.length(); ++i) {
                char ch = s.charAt(i);
                sb.append(ch - 'a' + 1);
            }

            String digits = sb.toString();
            for (int i = 1; i <= k && digits.length() > 1; i++) {
                int sum = 0;
                for (int j = 0; j < digits.length(); ++j) {
                    //相加求和
                    char ch = digits.charAt(j);
                    sum += ch - '0';
                }
                digits = Integer.toString(sum);
            }

            return Integer.valueOf(digits);
        }
    }
}
